﻿using System;
using System.ComponentModel.Composition;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.ObjectModel;
using ASPItalia.ModelVirtualCasting.Parts;

namespace ASPItalia.ModelVirtualCasting.EntityFramework
{
    [Export("RequestRepository", typeof(IRequestRepository))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    [RepositoryMetadata(Name = "RequestRepository", IsFake = false)]
    public class RequestRepository : BaseRepository<Request>, IRequestRepository
	{
        [ImportingConstructor()]
		public RequestRepository(IContextContainer container) : base(container)
		{
		}
		
		public void InsertOnSubmit(Request entity)
		{
			CurrentSet.Add(entity);
		}

		public void Update(Request entity)
		{
			CurrentSet.Attach(entity);
			Context.ChangeObjectState(entity, EntityState.Modified);
		}

		public void DeleteOnSubmit(Request entity)
		{
			CurrentSet.Remove(entity);
		}
		
		public void SubmitChanges()
		{
			SaveChanges();
		}

		public IQueryable<Request> GetAll()
		{
			return CurrentQueryableSet;
		}

		public IQueryable<Request> Where(Expression<Func<Request, bool>> predicate)
		{
			return CurrentQueryableSet.Where(predicate);
		}

		public long Count(Expression<Func<Request, bool>> predicate)
		{
			return CurrentQueryableSet.Count(predicate);
		}

		public IRequestRepository Include<TProperty>(Expression<Func<Request, TProperty>> path)
		{
			IncludeInternal(path);
			return this;
		}

		public IRequestRepository Include(string path)
		{
			IncludeInternal(path);
			return this;
		}

		public IRequestRepository CreateQuery()
		{
			CreateQueryInternal();
			return this;
		}

		public IRequestRepository EnableLazyLoading()
		{
			SetLazyLoading(true);
			return this;
		}

		public IRequestRepository DisableLazyLoading()
		{
			SetLazyLoading(false);
			return this;
		}

		public IRequestRepository DoNotTrack()
		{
			base.DoNotTrackInternal();
			return this;
		}
	}
}